<?php
namespace app\dk\admin;
use app\admin\controller\Admin;

use app\dk\model\Team as TeamMember;
use think\Request;
use think\Db;

class Team extends Admin
{
    public function index($q = '')
    {
        $map = [];
        if ($q) {
            // 用户名、昵称
                $map['name'] = ['like', '%'.$q.'%'];
            
        }
        
        $data_list = TeamMember::where($map)->order('isjs asc,isgf desc,usernum desc')->paginate();
		
        // 分页
        $pages = $data_list->render();
        $this->assign('data_list', $data_list);
        $this->assign('pages', $pages);
        return $this->fetch();
    }
	
	
    public function edit($id = 0)
    {
		
        if ($this->request->isPost()) {
            $data = $this->request->post();
           
            if (!TeamMember::update($data)) {
                return $this->error('修改失败！');
            }
            return $this->success('修改成功。');
        }
		
		$pm = $this->request->param();
		if( isset($pm["ql"]) && $pm["ql"]==1){
			Db::name('data')->where('tid', $id)->update(['step' => 0,'stepday'=>0]);
			return $this->success('修改成功2');
		}
		

        $row = TeamMember::where('id', $id)->find()->toArray();
        $this->assign('data_info', $row);
        return $this->fetch('form');
    }
	
	public function pm($id = 0)
    {
		$type = input("type");
		$tid = input("id");
		
		$q = input("q");
		$wh="";
		
		$tt="今日步数";
		if($type =="day"){
			if ($q) {
            // 用户名、昵称
                $wh=" where ( b.nickname like '%".$q."%' or c.phone like '%".$q."%' )";
			}
		
			$udate = date("Y-m-d");
			$sql="  SELECT  *,(@rownum:=@rownum+1) AS rownum from ( 
    SELECT a.*,b.nickname,b.imgurl,c.phone  from (
        SELECT a.uid, IFNULL(b.stepday,0) as stepday ,a.step,a.cdate from yd_data a
        left join (
            SELECT uid,stepday from yd_data where tid =$tid and udate = '$udate'
        ) b on a.uid = b.uid where a.tid = $tid
    ) a
left join yd_user_gl c on c.uid = a.uid 
    LEFT JOIN yd_user b on a.uid =b.id 
	$wh
	ORDER BY stepday desc ,cdate asc 
) a ,(SELECT @rownum:= 0 ) r   ";

			$data_list =Db::query($sql);//,['tid'=>$tid,'udate'=> date("Y-m-d")]
		}else{
			
			if ($q) {
				// 用户名、昵称
                $wh=" and ( nickname like '%".$q."%' or `yd_user`.phone like '%".$q."%' )";
			}
		
			$tt="个人累计";
			$sql=" SELECT  *,(@rownum:=@rownum+1) AS rownum from ( SELECT  `yd_data`.`uid`,`yd_data`.`step` as stepday ,`yd_data`.`udate`,`yd_user`.`nickname`,`yd_user`.`imgurl`,`yd_user_gl`.`phone` FROM `yd_data` INNER JOIN `yd_user` ON (`yd_data`.`uid` = `yd_user`.`id`) LEFT OUTER JOIN `yd_user_gl` ON (`yd_data`.`uid` = `yd_user_gl`.`uid`)  WHERE (`yd_data`.`tid` = $tid ) $wh ORDER BY `yd_data`.`step` DESC ) a ,(SELECT @rownum:= 0 ) r";
			$data_list =Db::query($sql);
		}
		
		
		
		$row = TeamMember::where('id', $id)->find()->toArray();
        $this->assign('data_list', $data_list);
		$this->assign('data_info', $row);
		$this->assign('type', $type);
		$this->assign('tt', $tt);
		$this->assign('tid', $tid);
		
        return $this->fetch('pm');
    }
	
}